package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.DepartmentDao;
import cn.gson.springboot.model.dao.FunctionTypeDao;
import cn.gson.springboot.model.mapper.FunctionTypeMapper;
import cn.gson.springboot.model.pojos.Department;
import cn.gson.springboot.model.pojos.Function;
import cn.gson.springboot.model.pojos.FunctionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FunctionTypeService {
    @Autowired
    FunctionTypeMapper ftm;
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    FunctionTypeDao functionTypeDao;
    public List<FunctionType> allFunTypes(){
        return ftm.allFunTypes();
    }

    //部门权限组
    public List<Integer> deptFunType(Integer deptId){
        return ftm.deptFunType(deptId);
    }

    //保存部门的权限组
    public void saveDeptJurisdiction(Integer deptId,List<Integer> funTypeId){
        //操作部门权限组的中间表
        Department department = departmentDao.findById(deptId).get();
        System.err.println(department);
        //获取中间表的集合
        List<FunctionType> funTypes = department.getFunctionType();
        if(funTypes == null){
            funTypes = new ArrayList<>();
        }
        funTypes.clear();//先清空
        //给集合添加数据，给表新增记录
        funTypes.addAll((Collection) functionTypeDao.findAllById(funTypeId));
        System.err.println(funTypes);
        System.err.println(funTypeId);
        //重置集合，相当于执行中间表新增
        department.setFunctionType(funTypes);
    }

    //
    public List<FunctionType> funMenu(Integer empId){
        return ftm.funMenu(empId);
    }

    public List<FunctionType> jobFunType(Integer jobId){
        return  ftm.jobFunType(jobId);
    }
    public List<Integer> jobFuns(Integer jobId){
        return  ftm.jobFuns(jobId);
    }
}
